//
//  newFeatureViewController.swift
//  XinLangWeibo10
//
//  Created by 白利兵 on 15/10/19.
//  Copyright © 2015年 itheima. All rights reserved.
//

import UIKit
import SnapKit
class newFeatureViewController: UICollectionViewController {
    var currentIndexPath: NSIndexPath?
    // 注册可重用cell标示符
    private let reuseID = "newFeatureCell"
    // 设置item数量
    private let newFeatureCellCount = 4
    // 构造方法
    init() {
        let layout = UICollectionViewFlowLayout()
        // 设置水平间距
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        // 设置大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        // 设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        super.init(collectionViewLayout: layout)
        // 上面的父类初始化之后才能初始化本类
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        // 注册可重用cell
        collectionView?.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseID)
    }
    
    
}

extension newFeatureViewController {

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return newFeatureCellCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        currentIndexPath = indexPath
        // 从缓存池中取cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! NewFeatureCell
        let image = UIImage(named: "new_feature_\(indexPath.item + 1)")
        cell.imageView.image = image
        return cell
    }
    // 监听滚动结束
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        // 计算当前页面索引值
        let page = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        // 获取当前cell
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        if page != newFeatureCellCount-1 {
            cell.startButton.hidden = true
            return
        }
        
        // 开启动画
        cell.startAnima()
        
    }
}




















